using Avalonia.Controls;
using Avalonia.Interactivity;
using IntegratedMeasuring.ViewModels.Scheme;
using System.Collections.Generic;
using Avalonia.Collections;
using IntegratedMeasuring.Utils;

namespace IntegratedMeasuring.Pages;

public partial class OrderSelectDialog : Window
{
    public ProductionOrder? SelectedOrder { get; private set; }
    private DataGridCollectionView? _orderView;

    public OrderSelectDialog(List<ProductionOrder> orders)
    {
        InitializeComponent();
        _orderView = new DataGridCollectionView(orders);
        _orderView.GroupDescriptions?.Clear();
        _orderView.GroupDescriptions?.Add(new DataGridPathGroupDescription("Name")); // 按工单号分组
        OrderDataGrid.ItemsSource = _orderView;
    }

    private void OkButton_Click(object? sender, RoutedEventArgs e)
    {
        if (OrderDataGrid.SelectedItem is ProductionOrder selected)
        {
            if (selected.PassNumber >= selected.PlanNumber && !selected.AllowOverflow)
            {
                NotificationHelper.ShowWarning("提示", "您选的工单周期已完成。");
                return;
            }
            SelectedOrder = selected;
            this.Close(true);
        }
    }

    private void CancelButton_Click(object? sender, RoutedEventArgs e)
    {
        this.Close();
    }
} 